package com.example.kingdeescan.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatCheckBox;

import com.example.kingdeescan.R;
import com.example.kingdeescan.dao.SEOutStock;
import com.example.kingdeescan.dto.ScanResultDTO;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author  : zly
 * e-mail  : 2444639551@qq.com
 * date    : 2020/7/9 11:23
 * alter   : zly
 * version : 1.0
 * desc    : 选单号适配器
 * address : Kingdee in Zhangzhou
 */
public class SelBillNoResultAdapter extends BaseAdapter {

    //存储扫码列表(用LinkedList实现，增删效率高)
    private List<SEOutStock> seOutStockList;
    private Context mContext;
    //ViewHolder通常出现在适配器里，为的是ListView滚动的时候快速设置值，而不必每次都重新创建很多对象，从而提升性能
    ViewHolderInner viewHolderInner;
    private int resourceId;
    //用来存放CheckBox的选中状态，true为选中,false为没有选中
    private SparseBooleanArray stateCheckedMap;

    /**
     * 传递上下文环境、数据集合、checkbox选中状态到适配器
     * @param context
     * @param dataList
     */
    public SelBillNoResultAdapter(Context context, int resource, List<SEOutStock> dataList, SparseBooleanArray stateCheckedMap) {
        this.mContext=context;
        this.resourceId=resource;
        this.seOutStockList =dataList;
        this.stateCheckedMap=stateCheckedMap;
    }

    @Override
    public int getCount() {
        return seOutStockList.size();
    }

    @Override
    public Object getItem(int position) {
        return seOutStockList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 重写获取视图，优化listview
     * @param position
     * @param convertView
     *      用于将之前加载好的布局进行缓存，便于重复利用
     *      如果convertView为null，则使用LayoutInflater去加载布局，不为空则直接重用convertView。
     * @param parent
     * @return View
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        SEOutStock seOutStock = (SEOutStock) getItem(position);//获取当前项的实例

        //如果convertView不为空，重用convertView
        if (null!=convertView){
            view=convertView;
            //重新获取ViewHolder（利用View的getTag()方法，把ViewHolder重新取出）
            viewHolderInner=(ViewHolderInner)view.getTag();
        }else{//如果convertView为空，则使用LayoutInflater()去加载布局
            //LayoutInflater的inflate()方法接收3个参数：需要实例化布局资源的id、ViewGroup类型视图组对象、false
            //false表示只让父布局中声明的layout属性生效，但不会为这个view添加父布局
            view= LayoutInflater.from(mContext).inflate(resourceId,parent,false);
            viewHolderInner=new SelBillNoResultAdapter.ViewHolderInner(view);

            //将ViewHolder存储在view中
            view.setTag(viewHolderInner);
        }
        //设置文字
        viewHolderInner.search_fselbillno_fdate.setText(new SimpleDateFormat("yyyy-MM--dd").format(new Date(seOutStock.getFDate().getTime())));
        viewHolderInner.search_fselbillno_FCustID.setText(String.valueOf(seOutStock.getFCustName()));
        viewHolderInner.search_fselbillno_FBillNo.setText(seOutStock.getFBillNo());

//        viewHolderInner.search_fselbillno_FItemName.setText();
//        viewHolderInner.search_fselbillno_FUnitID.setText();
//        viewHolderInner.search_fselbillno_FAuxQty.setText();
//        viewHolderInner.search_fselbillno_FPrice.setText();
//        viewHolderInner.search_fselbillno_FAmount.setText();
//        viewHolderInner.search_fselbillno_FFetchDate.setText();

        //设置checkbox的勾选状态
        viewHolderInner.checkBox.setChecked(stateCheckedMap.get(position));

        //返回view
        return view;
    }

    /**
     * 定义ViewHolderInner内部类，用于对控件实例进行缓存
     */
    public class ViewHolderInner{
        @BindView(R.id.search_fselbillno_fdate) public TextView search_fselbillno_fdate;
        @BindView(R.id.search_fselbillno_FCustID) public TextView search_fselbillno_FCustID;
        @BindView(R.id.search_fselbillno_FBillNo) public TextView search_fselbillno_FBillNo;
//        @BindView(R.id.search_fselbillno_FItemName) public TextView search_fselbillno_FItemName;
//        @BindView(R.id.search_fselbillno_FUnitID) public TextView search_fselbillno_FUnitID;
//        @BindView(R.id.search_fselbillno_FAuxQty) public TextView search_fselbillno_FAuxQty;
//        @BindView(R.id.search_fselbillno_FPrice) public TextView search_fselbillno_FPrice;
//        @BindView(R.id.search_fselbillno_FAmount) public TextView search_fselbillno_FAmount;
//        @BindView(R.id.search_fselbillno_FFetchDate) public TextView search_fselbillno_FFetchDate;
        @BindView(R.id.search_fselbillno_checked) public AppCompatCheckBox checkBox;

        public ViewHolderInner(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
